<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Product_library
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		if (!isset($this->CI))
		{
			$this->CI =& get_instance();
		}
		
		$this->_table = $this->CI->config->item('database_tables');
	}
		
	// Public methods
	public function filter_price()
	{
	?>
		<div class="relatedLinksBox">
			<div class="relatedDynamicLinksBox">
				<div class="titleContainer">Chọn sản phẩm theo giá</div>
				<ul class="leftTilePodLinks">
				      <li>
				      	<a target="_self" href="http://9vnd.net/product/filterprice/0/200000" id="ft_200">Từ 0 - 200,000</a>
				      </li>
				      <li>
				      	<a target="_self" href="http://9vnd.net/product/filterprice/200000/500000" id="ft_200-500">Từ 200,000 - 500,000</a></li>
				      <li>
				      	<a target="_self" href="http://9vnd.net/product/filterprice/500000/1000000" id="ft_500_1000">Từ 500,000 - 1,000,000</a></li>                            
				      <li>
				      	<a target="_self" href="http://9vnd.net/product/filterprice/1000000/2000000" id="ft_1000-2000">Từ 1,000,000 - 2,000,000</a></li>
				      <li>
				      	<a target="_self" href="http://9vnd.net/product/filterprice/2000000/4000000" id="ft_2000_4000">Từ 2,000,000 - 4,000,000</a></li>
				      <li>
				      	<a target="_self" href="http://9vnd.net/product/filterprice/4000000/0" id="ft_4000">Lớn hơn 4,000,000</a></li>              
				 </ul>
			</div>
		</div>
	<?php
	}
	public function get_feature_products($lang_id=null,$number=0)
	{
		if($lang_id == null)
		{
			$lang_id = 2; //default language is vietnamese
		}
		$this->CI->db->select('*');
		$this->CI->db->from($this->_table['products'] . ' products');
		$this->CI->db->where('lang_id',$lang_id);
		$this->CI->db->where('published','1');
		$this->CI->db->where('feature','1');
		$this->CI->db->order_by('id', 'ASC');
		if($number != 0)
			$this->CI->db->limit($number);
		
		$query = $this->CI->db->get();
			
		if ($query->num_rows() > 0)
		{			
			return $query->result_array();
		}
	}
	public function feature_product($lang_id=null,$number=0)
	{
		$rs = $this->get_feature_products($lang_id=null,$number=0);
		$html = '';
		$html = $html . '<div class="feature_product"><div class="feature_productTitle" ><div class="boldedLargeTextGrey">Sản phẩm nổi bật</div></div><ul>';
		foreach($rs as $item)
		{
			$html = $html .'<li>';
			$html = $html .'<div class="ft_product_image"><a href="'.  product_url($item['title'],$item['id']) . '.html"><img src="'.base_url().'uploads/products/small/'. $item['image'] .'" /></a></div>';
			$html = $html .'<div class="ft_product_description"><div class="name">'. $item['title'] .'</div> <div class="code"><b> Mã: '. $item['code'].'</b></div><div class="price">'.number_format($item['price']).' vnđ</div> </div>';
			$html = $html .'<div style="clear:both"></div>';
			$html = $html .'</li>';
		}			
		 $html = $html . '</ul></div>'; 
		
		return $html;
	}
	public function get_latest_product($lang_id=null,$number=0)
	{
		
	}
	public function get_categories($lang_id=null)
	{
		if($lang_id == null)
		{
			$lang_id = 2; //default language is vietnamese
		}
		$this->CI->db->select('id, name, url_name,status,title');
		$this->CI->db->from($this->_table['category'] . ' category');
		$this->CI->db->where('lang_id',$lang_id);
		$this->CI->db->where('status','1');
		$this->CI->db->select('(SELECT COUNT(' . $this->CI->db->dbprefix . 'newscategory.id) FROM ' . $this->CI->db->dbprefix . 'newscategory WHERE ' . $this->CI->db->dbprefix . 'newscategory.category_id = category.id) AS posts_count', FALSE); 
		$this->CI->db->order_by('id', 'ASC');
		
		$query = $this->CI->db->get();
			
		if ($query->num_rows() > 0)
		{			
			return $query->result_array();
		}
	}
	
	public function get_categories_by($lang_id=null,$parent_id)
	{
		if($lang_id == null)
		{
			$lang_id = 2; //default language is vietnamese
		}
		$this->CI->db->select('id, name, url_name,status,title');
		$this->CI->db->from($this->_table['category'] . ' category');
		$this->CI->db->where('lang_id',$lang_id);
		$this->CI->db->where('status','1');
		$this->CI->db->where('parent_id',$parent_id);
		$this->CI->db->select('(SELECT COUNT(' . $this->CI->db->dbprefix . 'newscategory.id) FROM ' . $this->CI->db->dbprefix . 'newscategory WHERE ' . $this->CI->db->dbprefix . 'newscategory.category_id = category.id) AS posts_count', FALSE); 
		$this->CI->db->order_by('id', 'ASC');
		
		$query = $this->CI->db->get();
			
		if ($query->num_rows() > 0)
		{			
			return $query->result_array();
		}
		
	}
}

/* End of file Categories_library.php */
/* Location: ./application/libraries/Categories_library.php */